package app.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import app.config.Config;
import app.domain.FixOrderHandle;
import app.domain.FixOrderStatus;
import app.domain.Message;
import app.domain.Result;
import app.domain.UserInfo;
import app.domain.Worker;
import app.service.FixOrderHandleService;
import app.service.FixOrderService;
import app.service.FixOrderStatusService;
import app.service.MessageService;
import app.utils.DateUtil;

@Controller
@RequestMapping("fixOrderHandle")
public class FixOrderHandleController {
	@Autowired
	private FixOrderHandleService  fixOrderHandleService;
	@Autowired
	private FixOrderStatusService fixOrderStatusService;
	@Autowired
	private FixOrderService fixOrderService;
	@Autowired
	private MessageService messageService;
	
	@RequestMapping("/addInfo")
	@ResponseBody
	public Result addInfo(HttpSession session,int fixOrderId,int examStatus,int isCharge,int charge,int repairerId,String fixTime,String remark,int accountId){
		//添加处理信息
		FixOrderHandle fixOrderHandle=new FixOrderHandle();
		fixOrderHandle.setFixOrderId(fixOrderId);
		fixOrderHandle.setExamStatus(examStatus);
		fixOrderHandle.setIsCharge(isCharge);
		fixOrderHandle.setCharge(charge);
		UserInfo currentUser=(UserInfo)session.getAttribute("userInfo");
		fixOrderHandle.setOperatorId(currentUser.getAccountId());
		fixOrderHandle.setRepairerId(repairerId);
		fixOrderHandle.setRemark(remark);
		Timestamp timestamp=null;
		java.util.Date date = null;
	
		Result result=new Result();
		try{
			date = DateUtil.StringToDate(fixTime, "yyyy-MM-dd HH:mm");
			if(date!=null){
				timestamp=new Timestamp(date.getTime());
			}
			fixOrderHandle.setFixTime(timestamp);
			fixOrderHandleService.add(fixOrderHandle);
			//更改订单状态
			fixOrderService.updateHandleStatus(fixOrderId,Config.FIXORDER_HANDLE_STATUS_ON_REPAIRE);
			//添加状态信息
			FixOrderStatus fixOrderStatus=new FixOrderStatus();
			fixOrderStatus.setFixOrderId(fixOrderId);
			fixOrderStatus.setOperation("订单处理");
			fixOrderStatus.setRemark("管理员查看并处理报修订单");
			fixOrderStatusService.addStatusInfo(fixOrderStatus);
			//通知用户和工人
			String contentToUser="编号为:"+fixOrderId+"的订单已被查看并处理";
			String contentToRepairer="编号为:"+fixOrderId+"的订单预定为您进行维修";
			Message messageToUser=new Message(contentToUser, accountId);
			Message messageToRepairer=new Message(contentToRepairer, repairerId);
			messageService.addMessage(messageToUser);
			messageService.addMessage(messageToRepairer);
			
			result.setCode(Config.SUCCESS_CODE);
		}
		catch (Exception e) {
			e.printStackTrace();
			result.setCode(Config.FAIL_CODE);
			return result;
		}
		return result;
	}
	@RequestMapping("/updateInfo")
	@ResponseBody
	public Result updateInfo( int fixOrderId,int examStatus,int isCharge,int charge,int repairerId,String fixTime,String remark,int accountId){
		FixOrderHandle fixOrderHandle=new FixOrderHandle();
		fixOrderHandle.setFixOrderId(fixOrderId);
		fixOrderHandle.setExamStatus(examStatus);
		fixOrderHandle.setIsCharge(isCharge);
		fixOrderHandle.setCharge(charge);
		fixOrderHandle.setRepairerId(repairerId);
		fixOrderHandle.setRemark(remark);
		Timestamp timestamp=null;
		java.util.Date date = null;
		
		Result result=new Result();
		try{
			date = DateUtil.StringToDate(fixTime, "yyyy-MM-dd HH:mm");
			if(date!=null){
				timestamp=new Timestamp(date.getTime());
			}
			fixOrderHandle.setFixTime(timestamp);
			fixOrderHandleService.update(fixOrderHandle);
			//添加状态信息
			FixOrderStatus fixOrderStatus=new FixOrderStatus();
			fixOrderStatus.setFixOrderId(fixOrderId);
			fixOrderStatus.setOperation("订单处理");
			fixOrderStatus.setRemark("管理员再次处理报修订单");
			fixOrderStatusService.addStatusInfo(fixOrderStatus);
			//通知用户和工人
			String contentToUser="编号为:"+fixOrderId+"的订单处理结果已被更新";
			String contentToRepairer="编号为:"+fixOrderId+"的订单预定为您进行维修，请在指定时间进行维修";
			Message messageToUser=new Message(contentToUser, accountId);
			Message messageToRepairer=new Message(contentToRepairer, repairerId);
			messageService.addMessage(messageToUser);
			messageService.addMessage(messageToRepairer);
			
			result.setCode(Config.SUCCESS_CODE);
		}
		catch (Exception e) {
			e.printStackTrace();
			result.setCode(Config.FAIL_CODE);
			return result;
		}
		return result;
	}
	
	
}
